package com.itany.boot.nmms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.boot.nmms.constant.DistinctConstant;
import com.itany.boot.nmms.entity.Dept;
import com.itany.boot.nmms.exception.DeptExistException;
import com.itany.boot.nmms.exception.RequestParamsException;
import com.itany.boot.nmms.service.DeptService;
import com.itany.boot.nmms.util.ParamsUtil;
import com.itany.boot.nmms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 部门控制器
 *
 * @author Tom
 * @date 2021/06/05
 */
@Controller
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @RequestMapping("/show")
    public ModelAndView show(String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        if (ParamsUtil.isNull(pageNo)) {
            pageNo = DistinctConstant.DEFAULT_PAGE;
        }

        if (ParamsUtil.isNull(pageSize)) {
            pageSize = DistinctConstant.DEFAULT_PAGESIZE;
        }

        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        List<Dept> depts = deptService.findAll();
        PageInfo<Dept> pageInfo = new PageInfo<>(depts);
        mav.setViewName("backend/deptManager");
        mav.addObject("pageInfo", pageInfo);
        return mav;
    }

    @RequestMapping("/addDept")
    @ResponseBody
    public ResponseResult addDept(String name, String duty, HttpSession session) {
        ResponseResult result = null;
        try {
            deptService.addDept(null, name, duty, session);
            result = ResponseResult.success();
        } catch (RequestParamsException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (DeptExistException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = ResponseResult.fail("服务器内部异常");
        }
        return result;


    }

    @RequestMapping("/findById")
    @ResponseBody
    public ResponseResult findById(String id) {
        ResponseResult result = null;
        try {
            Dept dept = deptService.findById(id);
            result = ResponseResult.success(dept);
        } catch (RequestParamsException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = ResponseResult.fail("服务器内部异常");
        }

        return result;
    }

    @RequestMapping("/addSonDept")
    @ResponseBody
    public ResponseResult addSonDept(String fatherId, String name, String duty, HttpSession session) {
        ResponseResult result = null;
        try {
            deptService.addDept(fatherId, name, duty, session);
        } catch (RequestParamsException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (DeptExistException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = ResponseResult.fail("服务器内部异常");
        }

        return result;
    }

    @RequestMapping("/doDisable")
    @ResponseBody
    public ResponseResult doDisable(String id) {
        ResponseResult result = null;
        try {
            deptService.modifyStatus(id);
            result = ResponseResult.success();
        } catch (RequestParamsException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = ResponseResult.fail("服务器内部异常");
        }

        return result;
    }

    @RequestMapping("/modifyDept")
    @ResponseBody
    public ResponseResult modifyDept(String id, String name, String duty) {
        ResponseResult result = null;
        try {
            deptService.modifyDept(id, name, duty);
            result = ResponseResult.success();
        } catch (RequestParamsException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (DeptExistException e) {
            result = ResponseResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result = ResponseResult.fail("服务器内部异常");
        }


        return result;
    }


}
